package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	ProfilePortraitSetRequest struct {
		FromAccount string                  `json:"From_Account"`
		ProfileItem []ProfilePortraitSetTag `json:"ProfileItem"`
	}
	ProfilePortraitSetTag struct {
		Tag   string `json:"Tag"`
		Value string `json:"Value"`
	}
	ProfilePortraitSetResponse struct {
		ActionStatus string `json:"ActionStatus"`
		ErrorCode    int    `json:"ErrorCode"`
		ErrorInfo    string `json:"ErrorInfo"`
		ErrorDisplay string `json:"ErrorDisplay"`
	}
)

func (s sdk) PortraitSetRequest(req ProfilePortraitSetRequest) (ok bool, resp ProfilePortraitSetResponse) {
	resp = ProfilePortraitSetResponse{}
	reqUrl := s.BuildRestfulUrl("/profile/portrait_set")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("request PortraitSetRequest error: ", err)
		return
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("request PortraitSetRequest Unmarshal error: ", err)
		return
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("request PortraitSetRequest error: ", err, "error message : ", resp.ErrorInfo, "req:", string(bytes))
		return
	}
	ok = true
	return
}

type (
	ProfilePortraitGetRequest struct {
		ToAccount []string `json:"To_Account"`
		TagList   []string `json:"TagList"`
	}
	ProfilePortraitGetResponse struct {
		ToAccount       []string `json:"To_Account"`
		TagList         []string `json:"TagList"`
		UserProfileItem []struct {
			ToAccount   string `json:"To_Account"`
			ProfileItem []struct {
				Tag   string `json:"Tag"`
				Value string `json:"Value"`
			} `json:"ProfileItem"`
			ResultCode int    `json:"ResultCode"`
			ResultInfo string `json:"ResultInfo"`
		} `json:"UserProfileItem"`
		ActionStatus string `json:"ActionStatus"`
		ErrorCode    int    `json:"ErrorCode"`
		ErrorInfo    string `json:"ErrorInfo"`
		ErrorDisplay string `json:"ErrorDisplay"`
	}
)

func (s sdk) PortraitGetRequest(req ProfilePortraitGetRequest) (ok bool, resp ProfilePortraitGetResponse) {
	resp = ProfilePortraitGetResponse{}
	reqUrl := s.BuildRestfulUrl("/profile/portrait_get")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("request PortraitGetRequest error: ", err)
		return
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("request PortraitGetRequest Unmarshal error: ", err)
		return
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("request PortraitGetRequest error: ", err, "error message : ", resp.ErrorInfo, "req:", string(bytes))
		return
	}
	ok = true
	return
}
